package cn.swenty.backstage.base.web.system.controller;

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.swenty.backstage.base.web.system.entity.SysResFormMap;
import cn.swenty.backstage.base.web.system.entity.SysRoleFormMap;
import cn.swenty.backstage.base.web.system.entity.SysRoleResFormMap;
import cn.swenty.backstage.base.web.system.mapper.FunctionMapper;
import cn.swenty.backstage.base.web.system.service.FunctionService;
import cn.swenty.backstage.base.web.system.service.RoleService;
import cn.swenty.backstage.framework.common.FormMap;
import cn.swenty.backstage.framework.common.model.Function;
import cn.swenty.backstage.framework.common.util.Bean2MapUtils;
import cn.swenty.backstage.framework.common.util.StringUtil;
import cn.swenty.backstage.framework.controller.BaseController;
import cn.swenty.backstage.framework.database.plugin.PageView;
import cn.swenty.backstage.framework.util.Common;
import cn.swenty.backstage.framework.util.IdGenerator;

/**
 * 菜单管理
 * @author liukang
 *
 */
@Controller
@RequestMapping("/system/function/")

public class FunctionController extends BaseController{
	@Inject
	private FunctionService functionService;
	
	
	@Inject
	private FunctionMapper functionMapper;
	
	@Autowired
	private RoleService roleService;
	
	
	
	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		
		
		
		return Common.BACKGROUND_PATH + "/system/function/list";
	}
	
	
	/**
	 * 查询当前级别菜单
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("functions")
	public PageView findByPage(String pageNow,
			String pageSize,String column,String sort) {
		SysResFormMap function = getFormMap(SysResFormMap.class);
		String functionId=getPara("parent_id");
		if(!functionId.equals("aaa")){
			function.put("parent_id", getPara("parent_id"));
		}
		if(StringUtil.isNull(function.getStr("orderby"))){
			function.put("orderby", " order by squence asc");
		}
		
		String state =(String) function.get("state");
		if(state==null)
			function.put("state", 0);   //查在职
		else if(state.equals("2")){
			//function.put("state", null);   //默认全讯全部
			function.remove("state");
		}
		
		function=toFormMap(function, pageNow, pageSize,function.getStr("orderby"));
		function.put("column", column);
		function.put("sort", sort);
		
		
		
		pageView.setRecords(functionService.findByPage(function));
		return pageView;
	}
	
	
	@RequestMapping("tree")
	public String tree() throws Exception{
		List<SysResFormMap> listFunction=	functionService.queryAll(SysResFormMap.class);
		List<Function> list =null;
		list = Bean2MapUtils.maps2Objects(listFunction, Function.class);
		Collections.sort(list);
		getHttpRequest().setAttribute("list",list);

		SysResFormMap sysResFormMap= new SysResFormMap();
		sysResFormMap.put("state", 1);
		List<SysResFormMap> hiddens= functionService.findResHiddens(sysResFormMap);
		String ress="";
		for (SysResFormMap string : hiddens) {
			if(string.get("function_id")!=null){
				ress +=string.get("function_id")+",";
			}
		}
		getHttpRequest().setAttribute("hiddenResTree", ress);
		
		return Common.BACKGROUND_PATH +"/system/function/tree";
	}
	
	
	
	
	
	@RequestMapping("queryLevel")
	@ResponseBody
	public Boolean queryLevel(Model model) throws Exception {
		String id = getPara("id");
		FormMap  bean= roleService.load(SysResFormMap.class, id);
		//当前对象
		Integer level= (Integer) bean.get("level");
		if(level==3){
			return false;
		}else{
			return true;
		}
	}
	
	
	
	

	/**
	 * 跳转到新增界面
	 * 
	 * @return
	 */
	@RequestMapping("addUI")
	public String addUI(Model model,String functionId) {
		SysResFormMap function = getFormMap(SysResFormMap.class);
		if(!functionId.isEmpty()){
			function=	functionService.get(SysResFormMap.class, functionId);
		}
		model.addAttribute("parentResources", function);
		return Common.BACKGROUND_PATH + "/system/function/info";
	}
	
	
	/**
	 * 添加菜单
	 * 
	 * @param resources
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping("addEntity")
	@ResponseBody
	public String addEntity() throws Exception {
		SysResFormMap sysResFormMap = getFormMap(SysResFormMap.class);
		sysResFormMap.put("function_id", IdGenerator.getNextId());
//		Integer sqe= functionService.queryMaxSqe();
//		sysResFormMap.put("squence", sqe);
		Object squence = sysResFormMap.get("squence");
		if(null==squence){
			sysResFormMap.set("state", "0");
		}
		
		Object o = sysResFormMap.get("state");
		if(null==o){
			sysResFormMap.set("state", "0");
		}
		functionService.save(sysResFormMap);
		return "success";
	}
	
	
	/**
	 * 跳转到修改界面
	 * 
	 * @param model
	 * @param resourcesId
	 *            修改菜单信息ID
	 * @return
	 */
	@RequestMapping("editUI")
	public String editUI(Model model) {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			FormMap  resources= functionService.load(SysResFormMap.class, id);
			
			String parent_id=(String) resources.get("parent_id");
			if(Common.isNotEmpty(parent_id)){
				SysResFormMap parentResources= new SysResFormMap();
				parentResources.put("function_id", parent_id);
				parentResources=functionMapper.findSysResFormMap(parentResources);
				model.addAttribute("parentResources", parentResources);
			}
			model.addAttribute("resources", resources);
		}
		return Common.BACKGROUND_PATH + "/system/function/info";
	}
	
	
	/**
	 * 更新菜单
	 * 
	 * @param model
	 * @param Map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("editEntity")
	public String editEntity(Model model) throws Exception {
		SysResFormMap sysResFormMap = getFormMap(SysResFormMap.class);
		if("2".equals(sysResFormMap.get("function_type"))){
			sysResFormMap.put("description", Common.htmltoString(sysResFormMap.get("description")+""));
		}
		Object o = sysResFormMap.get("state");
		if(null==o){
			sysResFormMap.set("state", "0");
		}
		functionService.editEntity(sysResFormMap);
		return "success";
	}

	
	
	@ResponseBody
	@RequestMapping("deleteEntity")
	public String delete(){
		String id = this.getPara("ids");
		String [] ids =id.split(",");
		functionService.deleteByIds(ids);
		return "success";
	}
	
	
	
	
	
	
	@RequestMapping("sortUpdate")
	@ResponseBody
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String sortUpdate(Model model) throws Exception {
		String ids = getPara("ids");
		//当前对象
		SysResFormMap  resources= functionService.get(SysResFormMap.class, ids);
		//当前序列
		Integer squence= (Integer) resources.get("squence");
		if(!resources.isEmpty()){
			SysResFormMap  sysResFormMap = getFormMap(SysResFormMap.class);
			sysResFormMap.put("where", " where parent_id = "+"'"+resources.get("parent_id")+"'  " +  " and  squence > "+ squence+ " order by squence " );
			//同父节点对象集合排序
			List<SysResFormMap>  list =functionService.queryList(sysResFormMap);
			//当前最大序列 对象
			System.out.println(list.size());
			if(list.size()==0){
				return "maxNum";
			}
			SysResFormMap	aimBean=list.get(0);
			resources.set("squence", aimBean.get("squence")); //当前 序列改为上移目标序列
			functionService.editEntity(resources);
			aimBean.set("squence", squence); //目标序列改为当前序列
			functionService.editEntity(aimBean);
			
		}
		return "success";
	}
	
	
	
	
	@RequestMapping("sortDown")
	@ResponseBody
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String sortDown(Model model) throws Exception {
		String ids = getPara("ids");
		//当前对象
		SysResFormMap  resources= functionService.get(SysResFormMap.class, ids);
		//当前序列
		Integer squence= (Integer) resources.get("squence");
		if(!resources.isEmpty()){
			SysResFormMap  sysResFormMap = getFormMap(SysResFormMap.class);
			sysResFormMap.put("where", " where parent_id = "+"'"+resources.get("parent_id")+"'  " +  " and  squence < "+ squence+ " order by squence desc" );
			//同父节点对象集合排序
			List<SysResFormMap>  list =functionService.queryList(sysResFormMap);
			//当前最大序列 对象
			if(list.size()==0){
				return "minNum";
			}
			SysResFormMap	aimBean=list.get(0);
			resources.set("squence", aimBean.get("squence")); //当前 序列改为上移目标序列
			functionService.editEntity(resources);
			aimBean.set("squence", squence); //目标序列改为当前序列
			functionService.editEntity(aimBean);
			
		}
		return "success";
	}
	
	
	
	/**
	 * 跳转到关联界面
	 * 
	 * @return
	 */
	@RequestMapping("roleUI")
	public String roleUI(Model model) {
		String functionId = getPara("functionId");
		SysRoleResFormMap  sysRoleResFormMap = getFormMap(SysRoleResFormMap.class);
		sysRoleResFormMap.put("where", " where function_id = "+"'"+functionId+"'");
		 //已关联角色
		 List<SysRoleResFormMap>  roles =functionService.queryList(sysRoleResFormMap);
		 SysRoleFormMap formMap = new SysRoleFormMap();
		 formMap.put("column", "sequence");
		 List<SysRoleFormMap> list =  roleService.findRolePage(formMap);
		 model.addAttribute("list", list);
		 model.addAttribute("listRoleId", roles);
		 model.addAttribute("functionId", functionId);
		return Common.BACKGROUND_PATH + "/system/function/roleResList";
	}
	
	
	/**
	 * 角色权限中间表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addRoleFunctionEntity")
	public String addRoleFunctionEntity(Model model){
			String []  roleIds = getParaValues("role_id");
			functionService.saveRoleFunction(roleIds, getPara("function_id"));
			return "success";
		
	}
	
}
